﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMovie.Models;

namespace MvcMovie.Controllers
{ 
    public class MoviesController : Controller
    {
        // instantiates a movie database context
        // You can use the MovieDBContext to query, edit, and delete movies.
        private MovieDBContext db = new MovieDBContext();

        // GET: /Movies/
        public ViewResult Index()
        {
            // db.Movies.ToList() is a Movie List
            return View(db.Movies.ToList());
        }

        // GET: /Movies/Details/5
        public ActionResult Details(int id = 0)
        {
            Movie movie = db.Movies.Find(id);
            if (movie == null)
            {
                return HttpNotFound(); 
            }
            return View(movie);
        }

        // GET: /Movies/Create
        public ActionResult Create()
        {
            return View();
        } 

        // POST: /Movies/Create
        [HttpPost]
        public ActionResult Create(Movie movie)
        {
            if (ModelState.IsValid)
            {
                db.Movies.Add(movie);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }
            return View(movie);
        }
        
        /// <summary>
        ///     show the edit movie form
        ///     GET: /Movies/Edit/5
        /// </summary>
        /// <param name="id">movie id, default 0</param>
        /// <returns>void</returns>
        public ActionResult Edit(int id = 0)
        {
            Movie movie = db.Movies.Find(id);
            // validation if there are not movie
            if (movie == null)
            {
                return HttpNotFound();
            }
            return View(movie);
        }

        // POST: /Movies/Edit/5
        [HttpPost, ActionName("Edit")]
        public ActionResult Edit(Movie movie)
        {
            if (ModelState.IsValid)
            {
                db.Entry(movie).State = EntityState.Modified;
                db.SaveChanges();
                // redirect to the Index method of the MoviesController
                return RedirectToAction("Index");
            }
            return View(movie);
        }

        // GET: /Movies/Delete/5
        public ActionResult Delete(int id = 0)
        {
            Movie movie = db.Movies.Find(id);
            if (movie == null)
            {
                return HttpNotFound();
            }
            return View(movie);
        }

        // POST: /Movies/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Movie movie = db.Movies.Find(id);
            db.Movies.Remove(movie);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        /// <summary>
        /// GET and POST: SearchIndex
        /// It displays a list of movies and the search movies form
        /// </summary>
        /// <param name="movieGenre"></param>
        /// <param name="searchString"></param>
        /// <returns></returns>
        public ActionResult SearchIndex(string movieGenre, string searchString)
        {
            //create a List OBJECT to hold movie genres from the database
            var GenreLst = new List<string>();
            //retrieves all the genres from the database, is a Collection
            var GenreQry = from d in db.Movies orderby d.Genre select d.Genre;

            //add all the distinct genres to the list
            GenreLst.AddRange(GenreQry.Distinct());
            ViewBag.movieGenre = new SelectList(GenreLst);

            var movies = from m in db.Movies select m;
            if (!String.IsNullOrEmpty(searchString))
            {
                movies = movies.Where(s => s.title.Contains(searchString));
            }

            if (!string.IsNullOrEmpty(movieGenre))
            {
                movies = movies.Where(x => x.Genre == movieGenre);
            }
            return View(movies);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}